//package com.javabasic.crawler;
//
///**
// * @Author xiongmin
// * @Description //TODO
// * @Date 2020/1/16 15:36
// * @Version 1.0
// * @link https://blog.csdn.net/qq_35890572/article/details/86003679
// **/
//
//import okhttp3.FormBody;
//import okhttp3.MediaType;
//import okhttp3.OkHttpClient;
//import okhttp3.Request;
//import okhttp3.RequestBody;
//import okhttp3.Response;
//import org.apache.commons.lang3.StringUtils;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
//import java.io.IOException;
//import java.net.SocketTimeoutException;
//import java.util.ArrayList;
//import java.util.Arrays;
//import java.util.List;
//import java.util.Map;
//import java.util.concurrent.TimeUnit;
//
///**
// * @author zhoupeng
// */
//public class OkHttpUtil {
//
//    private static final Logger logger = LoggerFactory.getLogger(OkHttpUtil.class);
//
//    private static OkHttpClient client;
//
//    private static final String DEFAULT_MEDIA_TYPE = "application/json; charset=utf-8";
//
//    private static final int CONNECT_TIMEOUT = 5;
//
//    private static final int READ_TIMEOUT = 7;
//
//    private static final String USER_MESSAGE = "userMessage";
//
//    private static final String DEVELOPER_MESSAGE = "developerMessage";
//
//    private static final String GET = "GET";
//
//    private static final String POST = "POST";
//
//    /**
//     * 单例模式  获取类实例
//     *
//     * @return client
//     */
//    private static OkHttpClient getInstance() {
//        if (client == null) {
//            synchronized (OkHttpClient.class) {
//                if (client == null) {
//                    client = new OkHttpClient.Builder()
//                            .connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS)
//                            .readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)
//                            .build();
//                }
//            }
//        }
//        return client;
//    }
//
//    public static String doGet(String url, String callMethod) throws HttpStatusException {
//
//        try {
//            long startTime = System.currentTimeMillis();
//            addRequestLog(GET, callMethod, url, null, null);
//
//            Request request = new Request.Builder().url(url).build();
//            // 创建一个请求
//            Response response = getInstance().newCall(request).execute();
//            int httpCode = response.code();
//            String result;
//            if (response.body() != null) {
//                result = response.body().string();
//                addResponseLog(httpCode, result, startTime);
//            } else {
//                throw new RuntimeException("exception in OkHttpUtil,response body is null");
//            }
//            return handleHttpResponse(httpCode, result, response);
//        } catch (Exception ex) {
//            handleHttpThrowable(ex, url);
//            return StringUtils.EMPTY;
//        }
//    }
//
//
//    public static String doPost(String url, String postBody, String mediaType, String callMethod) throws HttpStatusException {
//        try {
//            long startTime = System.currentTimeMillis();
//            addRequestLog(POST, callMethod, url, postBody, null);
//
//            MediaType createMediaType = MediaType.parse(mediaType == null ? DEFAULT_MEDIA_TYPE : mediaType);
//            Request request = new Request.Builder()
//                    .url(url)
//                    .post(RequestBody.create(createMediaType, postBody))
//                    .build();
//
//            Response response = getInstance().newCall(request).execute();
//            int httpCode = response.code();
//            String result;
//            if (response.body() != null) {
//                result = response.body().string();
//                addResponseLog(httpCode, result, startTime);
//            } else {
//                throw new IOException("exception in OkHttpUtil,response body is null");
//            }
//            return handleHttpResponse(httpCode, result, response);
//        } catch (Exception ex) {
//            handleHttpThrowable(ex, url);
//            return StringUtils.EMPTY;
//        }
//    }
//
//    public static String doPost(String url, Map<String, String> parameterMap, String callMethod) throws HttpStatusException {
//        try {
//            long startTime = System.currentTimeMillis();
//            List<String> parameterList = new ArrayList<>();
//            FormBody.Builder builder = new FormBody.Builder();
//            if (parameterMap.size() > 0) {
//                parameterMap.keySet().forEach(parameterName -> {
//                    String value = parameterMap.get(parameterName);
//                    builder.add(parameterName, value);
//                    parameterList.add(parameterName + ":" + value);
//                });
//            }
//
//            addRequestLog(POST, callMethod, url, null, Arrays.toString(parameterList.toArray()));
//
//            FormBody formBody = builder.build();
//            Request request = new Request.Builder()
//                    .url(url)
//                    .post(formBody)
//                    .build();
//
//            Response response = getInstance().newCall(request).execute();
//            String result;
//            int httpCode = response.code();
//            if (response.body() != null) {
//                result = response.body().string();
//                addResponseLog(httpCode, result, startTime);
//            } else {
//                throw new IOException("exception in OkHttpUtil,response body is null");
//            }
//            return handleHttpResponse(httpCode, result, response);
//
//        } catch (Exception ex) {
//            handleHttpThrowable(ex, url);
//            return StringUtils.EMPTY;
//        }
//    }
//
//
//    private static void addRequestLog(String method, String callMethod, String url, String body, String formParam) {
//        logger.info("===========================request begin================================================");
//        logger.info("URI          : {}", url);
//        logger.info("Method       : {}", method);
//        if (StringUtils.isNotBlank(body)) {
//            logger.info("Request body : {}", body);
//        }
//        if (StringUtils.isNotBlank(formParam)) {
//            logger.info("Request param: {}", formParam);
//        }
//        logger.info("---------------------------request end--------------------------------------------------");
//    }
//
//    private static void addResponseLog(int httpCode, String result, long startTime) {
//        long endTime = System.currentTimeMillis();
//        logger.info("Status       : {}", httpCode);
//        logger.info("Response     : {}", result);
//        logger.info("Time         : {} ms", endTime - startTime);
//        logger.info("===========================response end================================================");
//    }
//
//    private static String handleHttpResponse(int httpCode, String result, Response response) throws HttpStatusException {
//        if (response.isSuccessful()) {
//            return result;
//        }
//        HttpStatus httpStatus = HttpStatus.valueOf(httpCode);
//        if (result.contains(USER_MESSAGE) && result.contains(DEVELOPER_MESSAGE)) {
//            RestMessage restMessage = JsonUtil.fromJson(result, RestMessage.class);
//            throw new HttpStatusException(httpStatus, restMessage);
//        } else {
//            throw new HttpStatusException(httpStatus, result);
//        }
//
//    }
//
//    private static void handleHttpThrowable(Exception ex, String url) throws HttpStatusException {
//        if (ex instanceof HttpStatusException) {
//            throw (HttpStatusException) ex;
//        }
//        logger.error("OkHttp error url: " + url, ex);
//        if (ex instanceof SocketTimeoutException) {
//            throw new RuntimeException("request time out of OkHttp when do get:" + url);
//        }
//        throw new RuntimeException(ex);
//    }
//
//}
//
